package com.chengv.redismq.demo;

import com.chengv.redismq.core.RedisMQTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private RedisMQTemplate redisMQTemplate;

    /**
     * 广播模式
     * <p>
     * 多个消费者组，监听同一个主题，则每个消费者组都会收到消息
     */
    @GetMapping("/broadcastMode")
    public void broadcastMode() {
        redisMQTemplate.send(DemoMQTopicConstant.BROADCAST_MODE_DEMO, "540101199802088170");
    }

    /**
     * 集群模式
     * <p>
     * 多个消费者同一个消费者组，监听同一个主题，则只有其中一个消费者收到消息
     */
    @GetMapping("/clusterMode")
    public void clusterMode() {
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setId("120101198605227458");
        redisMQTemplate.send(DemoMQTopicConstant.CLUSTER_MODE_DEMO, demoEntity);
    }

    /**
     * 延迟消息
     */
    @GetMapping("/delayMessage")
    public void delayMessage() {
        redisMQTemplate.send(DemoMQTopicConstant.DELAY_MESSAGE_DEMO, "630201199504128020", 5, TimeUnit.SECONDS);
    }
}
